package com.jzo2o.customer.controller.consumer;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: liZhiJun
 * @CreateTime: 2025-09-26
 * @Description:
 * @Version: 1.0
 */

@Slf4j
@RestController
@RequestMapping("/consumer/address-book")
@RequiredArgsConstructor
@Api(tags = "用户端 - 地址簿相关接口")
public class AddressBookController {

    private final IAddressBookService addressBookService;

    /**
     * 添加地址
     */
    @PostMapping
    @ApiOperation("添加地址")
    public void sava(@RequestBody AddressBookUpsertReqDTO addressBookDto) {
        log.info("添加地址：{}", addressBookDto);
        addressBookService.addAddressBook(addressBookDto);
    }

    /**
     * 分页查询地址
     *
     * @param pageQueryReqDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询地址")
    public PageResult<AddressBook> page(AddressBookPageQueryReqDTO pageQueryReqDTO) {
        log.info("分页查询地址：{}", pageQueryReqDTO);
        Page<AddressBook> page = addressBookService.page(new Page<>(pageQueryReqDTO.getPageNo(), pageQueryReqDTO.getPageSize()),
                Wrappers.<AddressBook>lambdaQuery().eq(AddressBook::getUserId, UserContext.currentUserId()));
        return new PageResult<>(page.getPages(), page.getTotal(), page.getRecords());
    }

    /**
     * 查询地址详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询地址详情")
    public AddressBook get(@PathVariable Long id) {
        log.info("查询地址详情：{}", id);
        return addressBookService.getById(id);
    }

    @PutMapping("/{id}")
    @ApiOperation("修改地址")
    public void update(@PathVariable Long id, @RequestBody AddressBookUpsertReqDTO addressBookDto) {
        log.info("修改地址：id{},详情 {}", id, addressBookDto);
        addressBookService.updateAddressBook(id, addressBookDto);
    }

    /**
     * 批量删除地址
     * @param ids ids
     */
    @DeleteMapping("/batch")
    @ApiOperation("批量删除地址")
    public void deleteBatch(@RequestBody String ids) {
        log.info("批量删除地址：{}", ids);
        if (!StringUtils.hasText(ids)) {
            return;
        }
        List<String> list = Arrays.asList(ids.split(","));
        addressBookService.removeByIds(list);
    }

    /**
     * 设置默认地址
     * @param flag 0：取消默认，1：设为默认
     * @param id 地址id
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void setDefault(@RequestParam(required = true) Integer flag, @RequestParam(required = true) Long id) {
        log.info("设置默认地址：flag{},id{}", flag, id);
        addressBookService.setDefault(flag, id);
    }

    @GetMapping("/defaultAddress")
    @ApiOperation("查询默认地址")
    public AddressBook getDefaultAddress() {
        log.info("查询默认地址");
        return addressBookService.getDefaultAddress();
    }

}
